/*
 * Created on 7/Abr/2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package org.cyborgas.xmltool.actions;

import java.util.Iterator;
import java.util.List;

import org.cyborgas.xmltool.MainWindow;
import org.cyborgas.xmltool.tabsheets.XPathTab;
import org.dom4j.Node;
import org.eclipse.jface.action.Action;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;

/**
 * @author rino
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class XpathCopyResultsAction extends Action {
	private MainWindow window;
	private XPathTab tab;

	/**
	 * @param treeViewer
	 * @param level
	 */
	public XpathCopyResultsAction(MainWindow window, XPathTab tab) {
		this.window = window;
		this.tab = tab;
		setText("&Copy Results");
		setToolTipText("Copy results of XPath query to clipboard");
//		setImageDescriptor(Util.getImageDescriptor("fileopen.gif"));
	}

	public void run() {
		StringBuffer result = new StringBuffer();
		List list = this.tab.getResults();
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			Object item = iterator.next();
			if (item instanceof Node) {
				result.append(((Node) item).getText());
			} else {
				result.append(item.toString());
			}
			result.append('\n');
		}

		Clipboard clipboard = new Clipboard(window.getShell().getDisplay());
		clipboard.setContents(new Object[] { result.toString() },
				new Transfer[] { TextTransfer.getInstance() });
	}

}
